package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MyActivity1 extends AppCompatActivity {
    private static final String TAG = "MyActivity1";
    private Button button_my1;
    private Button btn_jni_my1;

    /**
     * 测试如何通过jni调用这个函数
     * @param msg
     * @return
     */
    int onSend(byte[] msg) {
        if (msg!= null) {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            for (int i = 0; i < msg.length; i++) {
                sb.append(msg[i]);
                if (i < msg.length - 1) {
                    sb.append(", ");
                }
            }
            sb.append("]");
            System.out.println("Received message content: " + sb.toString());
        } else {
            System.out.println("Received null message.");
        }
        return 0;
    }

    /**
     * 测试java如何调用 onSend(byte[] msg)
     */
    void onSend_t(){
        byte[] msg = {1, 2, 3, 4, 5};
        int ret = onSend(msg);

    }
    private void find_layout_listen() {
        button_my1 = findViewById(R.id.button_my1);
        button_my1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity1.this, "Button被点击了", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
                Intent intent = new Intent(MyActivity1.this, MainActivity.class);
                startActivity(intent);
            }
        });

        btn_jni_my1 = findViewById(R.id.btn_jni_my1);
        btn_jni_my1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
//                test_jni();
                onSend_t();
            }
        });


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my1);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        find_layout_listen();
        onSend_t();
//        test_jni();
    }

    /**
     * 测试JNI，java调用c++
     */
    private void test_jni() {
        test1_t();
        testString_String_t();//java给c++传递字符串
        testArray_Array_t();//java给c++传递数组

        TestCppCallJava_t();//c++调用java，主要观察如何获得java中的类、方法、属性
        TestIData_t();//c++调用java时，传递自定义数据类型
        TestArrayList_t();//Java C++传递ArrayList
        TestRegCpp_t();
    }

    class MyClass {
    };

    public native void test1(short s, int i, long l, float f, double d, char c, boolean z, byte b, String str, Object obj, MyClass p, int[] arr);

    private void test1_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());

        short s = 1;
        long l = 20;
        byte b = 127;
        String obj = "obj";
        test1(s, 2, l, 1.0f, 10.5d, 'A', true, b, "国家队", obj, new MyClass(), new int[]{1, 2});

        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }

    public native String testString_String(String str);

    private void testString_String_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());

        String obj = "obj";
        String rtn = testString_String(obj);
        Log.d(TAG, "rtn = " + rtn);

        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }

    public native int[] testArray_Array(int[] arr);

    private void testArray_Array_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());

        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        String str = "";
        for (int i : arr) {
            str += i + " ";
        }
        Log.d(TAG, "before arr = " + str);

        int ret[] = testArray_Array(arr);
        str = "";
        for (int i : arr) {
            str += i + " ";
        }
        Log.d(TAG, "after arr = " + str);

        str = "";
        for (int i : ret) {
            str += i + " ";
        }
        Log.d(TAG, "ret = " + str);

        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }


    public static int m_int = 20;

    public static String JavaForCpp(String str, int i) {
        Log.d(TAG, "In java:para str = " + str + ", i = " + i);
        return "java return string 001";
    }

    public native String TestCppCallJava();

    //测试c++调用java的静态成员变量
    private void TestCppCallJava_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
        Log.d(TAG, "in java [before]: m_int = " + m_int);
        String str = TestCppCallJava();
        Log.d(TAG, "in java [after]: m_int = " + m_int + ", str = " + str);
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }

    public native IData TestIData(IData data);

    private void TestIData_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
        IData data = new IData();
        data.m_txt = "from java Data txt";
        Log.d(TAG, "in java [before]: data.m_txt = " + data.m_txt);
        IData ret = TestIData(data);
        Log.d(TAG, "in java [after]: data.m_txt = " + data.m_txt);
        Log.d(TAG, "in java [after]: ret.m_txt = " + ret.m_txt);
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }


    /**
     * Java C++传递ArrayList
     */
    public native ArrayList<String> TestArrayList(ArrayList<String> as);

    /**
     * 测试 Java C++传递ArrayList
     */
    private void TestArrayList_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
        ArrayList<String> arrs = new ArrayList<>();
        arrs.add("Java Data 001");
        arrs.add("Java Data 002");
        arrs.add("Java Data 003");
        ArrayList<String> res = TestArrayList(arrs);
        for (String s : res) {
            Log.d(TAG, "in java: re = " + s);
        }
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }

    /**
     * 动态注册jni绑定的函数
     *
     * @param hss
     * @return
     */
    private native HashMap<String, String> TestRegCpp(HashMap<String, String> hss);

    /**
     * 测试 动态注册jni绑定的函数
     */
    private void TestRegCpp_t() {
        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
        HashMap<String, String> hss = new HashMap<>();
        hss.put("java key001", "java value001");
        hss.put("java key002", "java value002");
        HashMap<String, String> res_hs = TestRegCpp(hss);
        //获取所有的key
        Set<String> ss = res_hs.keySet();

        Iterator<String> ssit = ss.iterator();
        while (ssit.hasNext()) {
            String s = ssit.next();
            Log.d(TAG, "in java: ssit.next() = " + s);
            String v = res_hs.get(s);
            Log.d(TAG, "in java: res_hs.get(s) = " + v);
        }

        Log.d(TAG, "class:" + getClass().getSimpleName() + ", func:" + new Throwable().getStackTrace()[0].getMethodName() + ", line:" + new Throwable().getStackTrace()[0].getLineNumber());
    }

}